package com.bys.ots.config;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.bys.ots.dao.UserFlowMapper;
import com.bys.ots.dao.UserMapper;
import com.bys.ots.model.Complaint;
import com.bys.ots.model.User;
import com.bys.ots.model.UserFlow;
import com.bys.ots.pojo.Result;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.UUID;

@Aspect
@Component
public class UserFlowAspect {
    @Resource
    private UserFlowMapper userFlowMapper;
    @Resource
    private UserMapper userMapper;

    //1,登录2，查看订单详情，3，查看国际订单物流，4，查看订单物流5，用户反馈记录
    @Pointcut("execution(public * com.bys.ots.controller.UserController.*(..))")
    public void getUserLogin() {
    }

    @AfterReturning(returning = "ret", pointcut = "getUserLogin()")
    public void getUserLogin(JoinPoint joinPoint, Object ret) throws Throwable {
        UserFlow userFlow = new UserFlow();
        String uuid = UUID.randomUUID().toString();
        userFlow.setId(uuid);
        String userId = "";
        Result result = (Result) ret;
        if (joinPoint.getSignature().getName().contains("login")) {
            userFlow.setOperate("1");
            userFlow.setCatagory("v1/login");
            userFlow.setDescription("用户登录");
            if(!StringUtils.isEmpty(result.getData())){
                userId = result.getData().toString();
            }

            if(!StringUtils.isEmpty(userId)){
                userFlow.setUserId(userId);
                User user = userMapper.findByUserId(userId);
                if(!ObjectUtils.isEmpty(user)){
                    userFlow.setUserName(user.getUserName());
                }
            }
            userFlowMapper.insert(userFlow);
        }

    }

    @Pointcut("execution(public * com.bys.ots.controller.OrderController.*(..))")
    public void getSoInfo() {
    }

    @Before("getSoInfo()")
    public void getSoInfo(JoinPoint joinPoint) throws Throwable {
        UserFlow userFlow = new UserFlow();
        String uuid = UUID.randomUUID().toString();
        userFlow.setId(uuid);
        String userId = "";
        String so = "";
        if (joinPoint.getSignature().getName().contains("getSoInfoDetail")) {
            userFlow.setOperate("2");
            userFlow.setCatagory("v1/getSoInfoDetail");
            userFlow.setDescription("查询订单詳情");
            userId = joinPoint.getArgs()[0].toString();
            so = joinPoint.getArgs()[1].toString();
            userFlow.setUserId(userId);
            if(!StringUtils.isEmpty(userId)){
                User user = userMapper.findByUserId(userId);
                if(!ObjectUtils.isEmpty(user)){
                    userFlow.setUserName(user.getUserName());
                }
            }
            userFlow.setSo(so);
            userFlow.setArgs(Arrays.asList(joinPoint.getArgs()).toString());
            userFlowMapper.insert(userFlow);
        }
        if (joinPoint.getSignature().getName().contains("getSoLogisticInfo")) {
            userFlow.setOperate("3");
            userFlow.setCatagory("v1/getSoLogisticInfo");
            userFlow.setDescription("查询国内物流信息");
             if(!StringUtils.isEmpty(joinPoint.getArgs()[0])){
                 userId = joinPoint.getArgs()[0].toString();
             }
            if(!StringUtils.isEmpty(joinPoint.getArgs()[2])) {
                so = joinPoint.getArgs()[2].toString();
            }
            if(!StringUtils.isEmpty(userId)){
                userFlow.setUserId(userId);
                User user = userMapper.findByUserId(userId);
                if(!ObjectUtils.isEmpty(user)){
                    userFlow.setUserName(user.getUserName());
                }
            }
            userFlow.setSo(so);
            userFlow.setArgs(Arrays.asList(joinPoint.getArgs()).toString());
            userFlowMapper.insert(userFlow);
        }
        if (joinPoint.getSignature().getName().contains("getLogisticInfo")) {
            userFlow.setOperate("4");
            userFlow.setCatagory("v1/getLogisticInfo");
            userFlow.setDescription("查询物流信息");
            if(!StringUtils.isEmpty(joinPoint.getArgs()[0])){
                userId = joinPoint.getArgs()[0].toString();
            }
            if(!StringUtils.isEmpty(joinPoint.getArgs()[2])) {
                so = joinPoint.getArgs()[2].toString();
            }
            if(!StringUtils.isEmpty(userId)){
                userFlow.setUserId(userId);
                User user = userMapper.findByUserId(userId);
                if(!ObjectUtils.isEmpty(user)){
                    userFlow.setUserName(user.getUserName());
                }
            }
            userFlow.setSo(so);
            userFlow.setArgs(Arrays.asList(joinPoint.getArgs()).toString());
            userFlowMapper.insert(userFlow);
        }
    }


    @Pointcut("execution(public * com.bys.ots.controller.ComplaintController.*(..))")
    public void getUserInsertComplaint() {
    }

    @Before("getUserInsertComplaint()")
    public void getUserInsertComplaint(JoinPoint joinPoint) throws Throwable {
        UserFlow userFlow = new UserFlow();
        String uuid = UUID.randomUUID().toString();
        userFlow.setId(uuid);
        Complaint complaint = null;
        String userId = "";
        String so = "";
        if (joinPoint.getSignature().getName().contains("insertComplaint")) {
            userFlow.setOperate("5");
            userFlow.setCatagory("v1/insertComplaint");
            userFlow.setDescription("用户投诉");
            complaint = (Complaint) joinPoint.getArgs()[0];
            userId = complaint.getAuthorId();
            so = complaint.getSo();
            if(!StringUtils.isEmpty(userId)){
                userFlow.setUserId(userId);
                User user = userMapper.findByUserId(userId);
                if(!ObjectUtils.isEmpty(user)){
                    userFlow.setUserName(user.getUserName());
                }
            }
            userFlow.setSo(so);
            userFlow.setArgs(complaint.toString());
            userFlowMapper.insert(userFlow);
        }
    }
}
